<template>
  <el-container>
    <el-aside width="200px">
      <el-menu default-active="1" class="el-menu-vertical-demo">
        <el-submenu index="1">
          <template slot="title">
            <i class="el-icon-s-tools"></i>
            <span>系统管理</span>
          </template>
          <el-menu-item index="1-1">
            <router-link to="/admin/user" tag="span">用户管理</router-link>
          </el-menu-item>
          <el-menu-item index="1-2">角色管理</el-menu-item>
          <el-menu-item index="1-3">日志管理</el-menu-item>
        </el-submenu>
      </el-menu>
    </el-aside>
    <el-container>
      <el-header class="header-container">
        <!-- 头部左侧可以放logo或系统名称 -->
        <div class="header-left">管理系统</div>

        <!-- 头部右侧用户区域 -->
        <div class="header-right">
          <el-dropdown trigger="click" @command="handleCommand">
            <div class="user-info">
              <img :src="user.avatar" alt="用户头像" class="user-avatar">
              <span class="user-name">{{user.username}}</span>
              <i class="el-icon-arrow-down user-arrow"></i>
            </div>
            <el-dropdown-menu slot="dropdown" class="user-dropdown">
              <el-dropdown-item command="center">
                <i class="el-icon-user"></i>
                <span>个人中心</span>
              </el-dropdown-item>
              <el-dropdown-item command="logout">
                <i class="el-icon-switch-button"></i>
                <span>退出登录</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-header>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import {mapState} from "vuex";
export default {
  name: 'HomeView',
  data() {
    return {
    }
  },
  computed:mapState(["user"]),
  methods: {
    // 处理下拉菜单命令
    handleCommand(command) {
      this.$router.push("/")
      if (command === 'center') {
        // 跳转到个人中心逻辑
        this.$message.info('前往个人中心');
        // 实际应用中可以使用路由跳转
        // this.$router.push('/personal-center');
      } else if (command === 'logout') {
        // 退出登录逻辑
        this.$confirm('确定要退出登录吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message.success('退出登录成功');
          this.$store.commit("saveUserInfo", null);
        }).catch(() => {
          this.$message.info('已取消退出');
        });
      }
    }
  }
}
</script>

<style scoped>
.header-container {
  background-color: #B3C0D1;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  height: 60px;
}

.header-left {
  font-size: 18px;
  font-weight: bold;
  color: #333;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-info:hover {
  background-color: rgba(255, 255, 255, 0.2);
}

.user-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  margin-right: 10px;
  border: 2px solid rgba(255, 255, 255, 0.8);
}

.user-name {
  color: #333;
  font-size: 14px;
  margin-right: 5px;
}

.user-arrow {
  font-size: 12px;
  color: #333;
}

.user-dropdown .el-dropdown-item {
  display: flex;
  align-items: center;
}

.user-dropdown .el-dropdown-item i {
  margin-right: 8px;
  width: 16px;
  text-align: center;
}

.el-aside {
  background-color: #FFCA28;
  color: #B3C0D1;
  height: 900px;
}

.el-menu-vertical-demo {
  background-color: transparent;
  border-right: none;
}

.el-menu-vertical-demo .el-submenu__title {
  color: #fff;
}

.el-menu-vertical-demo .el-menu-item {
  color: #fff;
}

.el-main {
  background-color: #E9EEF3;
  color: #333;
  text-align: center;
  line-height: 160px;
}

body > .el-container {
  margin-bottom: 40px;
}
</style>
